package com.bestcem.xm.om.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.om.dao.OmRoleDao;
import com.bestcem.xm.om.dao.OmUserDao;
import com.bestcem.xm.om.dao.OmUserIndustryDao;
import com.bestcem.xm.om.entity.OmRole;
import com.bestcem.xm.om.entity.OmUser;
import com.bestcem.xm.om.entity.OmUserIndustry;
import com.bestcem.xm.om.enums.OmUserStatusEnum;
import com.bestcem.xm.om.grpc.service.converter.OmUserConverter;
import com.bestcem.xm.om.service.OmUserIndustryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * om行业
 *
 * @author xuan.yang <xuan.yang@idiaoyan.com>
 * @version v1.0
 * @date 2021/12/29 16:26
 */
@Slf4j
@Service
public class OmUserIndustryServiceImpl implements OmUserIndustryService {
    @Resource
    private OmUserDao omUserDao;
    @Resource
    private OmUserIndustryDao omUserIndustryDao;
    @Resource
    private OmRoleDao omRoleDao;

    @Override
    public ServiceResult<OmUser> getIndustryPrincipal(String industryId) {
        // 通过industry表查询行业
        List<OmUserIndustry> industryList = omUserIndustryDao.listByIndustryId(industryId);
        if (CollUtil.isEmpty(industryList)) {
            return ServiceResult.fail("获取行业失败");
        }
        // 获取所有的userId
        List<String> userIds = industryList.stream()
                .map(OmUserIndustry::getOmUserId)
                .collect(Collectors.toList());
        List<OmUser> users = omUserDao.listByIdsAndStatus(userIds, OmUserStatusEnum.ENABLE.getIndex());
        List<OmRole> saleLeaders = omRoleDao.getSaleLeader();
        if (CollUtil.isEmpty(saleLeaders)) {
            if (log.isInfoEnabled()) {
                log.info("[om]获取角色失败");
            }
            return ServiceResult.fail("获取角色失败");
        }
        if (CollUtil.isEmpty(users)) {
            if (log.isInfoEnabled()) {
                log.info("[om]获取用户失败");
            }
            return ServiceResult.fail("获取用户失败");
        }
        Set<String> roleIds = saleLeaders.stream()
                .map(OmRole::getId).collect(Collectors.toSet());
        for (OmUser user : users) {
            if (roleIds.contains(user.getRoleId())) {
                if (log.isInfoEnabled()) {
                    log.info("[om]返回的用户信息{}", user);
                }
                //  获取到一个即可返回
                return ServiceResult.success(user);
            }
        }
        return ServiceResult.fail("获取角色信息失败");
    }

    @Override
    public ServiceResult<List<com.bestcem.xm.om.grpc.v1.services.OmUserIndustry>> listUserIndustries(OmUserIndustry userIndustry) {
        List<OmUserIndustry> omUserIndustries = omUserIndustryDao.listByCondition(userIndustry);
        if (CollectionUtils.isEmpty(omUserIndustries)) {
            return ServiceResult.fail("查询信息为空");
        }
        List<com.bestcem.xm.om.grpc.v1.services.OmUserIndustry> userIndustries = omUserIndustries.stream().map(OmUserConverter::convertToGrpcIndustry).collect(Collectors.toList());
        return ServiceResult.success(userIndustries);
    }

}
